package DataLayer.KnowledgeBase;

import ControllerLayer.ControlServices.Normalizer.INormalizer;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.Serializable;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import javax.swing.ImageIcon;

public class KnowledgeBase implements Serializable, IKnowledgeBase{

    public Map<String, SingleCharInfos> charInfoMap;

    /**
     * The constructor.
     */
    public KnowledgeBase(){

        charInfoMap = new HashMap<String, SingleCharInfos>();
        for (char ch = 'a'; ch <= 'z'; ++ch)
        {
            SingleCharInfos charInfo = new SingleCharInfos(ch);
            String key = Character.toString(ch);
            charInfoMap.put(key, charInfo);
        }
        for (char ch = 'A'; ch <= 'Z'; ++ch)
        {
            SingleCharInfos charInfo = new SingleCharInfos(ch);
            String key = Character.toString(ch);
            charInfoMap.put(key, charInfo);
        }
    }

    /**
     * Adds a new image to the knowledgebase.
     * @param sbuffImg - The image to be added.
     * @param ch - The character.
     * @param normalizer - The normalizer component.
     * @throws NoSuchAlgorithmException
     */
    public void addNewImage(SerializableBufferedImage sbuffImg, String ch, INormalizer normalizer) throws NoSuchAlgorithmException {
        ////System.out.println("A karakter:---- " + ch);
        if (!charInfoMap.get(ch).imgList.containsKey(sbuffImg.getID()))
        {
            ////System.out.println("nem tartalmazza: " + ch + " ID: " + sbuffImg.getID());
            charInfoMap.get(ch).imgList.put(sbuffImg.getID(), sbuffImg);
            charInfoMap.get(ch).updateAverageImage(sbuffImg.getID(), normalizer);
            ////if (!charInfoMap.get(ch).imgList.containsKey(sbuffImg.getID()))
                ////System.out.println("nem tartalmazza most se: " + ch + " ID: " + sbuffImg.getID());
        }
    }

    //törölni TESZT
    public SerializableBufferedImage loadImage(String imagePath) throws IOException, NoSuchAlgorithmException {
        ImageIcon icon = null;
        Image image = null;
        BufferedImage buffImage = null;
        try {
            icon = new ImageIcon(imagePath);
            image = icon.getImage();
            // Create empty BufferedImage, sized to Image
            buffImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
            // Draw Image into BufferedImage
            Graphics g = buffImage.getGraphics();
            g.drawImage(image, 0, 0, null);
        } catch (Exception ex) {
            System.out.print(ex);
        }

        SerializableBufferedImage output = new SerializableBufferedImage(buffImage);
        return output;
    }
}
